package com.newlink.building.record.adapter

import android.text.TextUtils
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.newlink.building.common_base.utils.TimestampUtils
import com.newlink.building.record.R
import com.newlink.building.record.fragment.CaptureVideoBean

/**
 * @author wangyang
 * @date 2022/10/8
 * @description 截图记录的Adapter
 */
class Module_Record_CaptureVideoAdapter(data: MutableList<CaptureVideoBean>) :
    BaseQuickAdapter<CaptureVideoBean, BaseViewHolder>(
        R.layout.item_capture_video, data
    ), LoadMoreModule {

    private var splitDeviceName: String = ""
    private var splitDate: String = ""
    private var mEditMode: Boolean = false

    fun toggleSelect(position: Int) {
        data[position].isSelected = !data[position].isSelected
        notifyItemChanged(position)
//        notifyDataSetChanged()
    }

    fun setEditMode(editMode: Boolean) {
        this.mEditMode = editMode
        notifyDataSetChanged()
    }

    fun setSelectAll(allSelect: Boolean) {
        for (bean in data) {
            bean.isSelected = allSelect
        }
        notifyDataSetChanged()
    }

    override fun convert(holder: BaseViewHolder, bean: CaptureVideoBean) {
        // /storage/emulated/0/Android/data/com.vdroid.dooraccess/files/Pictures
        val item = bean.path

        if (item.contains("/")) {
            val split1 = item.split("/")
            val fileName = split1[split1.size - 1]
            if (fileName.contains("@@@")) {
                val split2 = fileName.split("@@@")
                splitDeviceName = split2[0]
                if (split2.size > 1) {
                    val split3 = split2[1]
                    if (split3.contains(".")) {
                        splitDate = split3.split(".")[0]
                    }
                }
            }
        }

        Glide
            .with(context)
            .load(item)
            .placeholder(R.color.grey400)
            .transform(MultiTransformation(CenterCrop()))
            .into(holder.getView(R.id.iv_snapshot))
        holder.setText(R.id.tv_name, splitDeviceName)
        holder.getView<ImageView>(R.id.iv_snapshot).alpha = if (bean.isSelected) 0.5f else 1f
        holder.getView<CheckBox>(R.id.cb_select).apply {
            visibility = if (mEditMode) View.VISIBLE else View.GONE
            isChecked = bean.isSelected
        }

        if (!TextUtils.isEmpty(splitDate)) {
            try {
                holder.setText(
                    R.id.tv_time,
                    TimestampUtils.callLogTimeFormat(splitDate.toLong(), false)
                )
            } catch (e: Exception) {
            }
        }
    }


}